package com.foreverwin.mes.warehouse.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.common.dto.PrintDto;
import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.common.model.PrintLog;
import com.foreverwin.mes.common.service.PrintLogService;
import com.foreverwin.mes.common.service.PrintService;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.warehouse.dto.ReprintDto;
import com.foreverwin.mes.warehouse.mapper.ReprintMapper;
import com.foreverwin.mes.warehouse.service.ReprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Ervin Chen
 * @date 2020/3/6 14:04
 */
@Service
public class ReprintServiceImpl implements ReprintService {

    @Autowired
    private PrintService printService;
    @Autowired
    private PrintLogService printLogService;
    @Autowired
    private ReprintMapper reprintMapper;

    @Override
    public List<PrintLog> getPrinterList() {
        String site = CommonMethods.getSite();
        return reprintMapper.selectPrinterList(site);
    }

    @Override
    public List<PrintLog> getPrintTemplateList() {
        String site = CommonMethods.getSite();
        return reprintMapper.selectPrintTemplateList(site);
    }

    @Override
    public Page<PrintLog> getPrintLogList(FrontPage<PrintLog> frontPage, ReprintDto reprintDto) {
        String site = CommonMethods.getSite();
        String printer = reprintDto.getPrinter();
        String printTemplate = reprintDto.getPrintTemplate();
        String createUser = reprintDto.getCreateUser();
        Date startDate = reprintDto.getStartDateTime();
        Date endDate = reprintDto.getEndDateTime();
        if (startDate != null && endDate != null && startDate.after(endDate)) {
            throw BusinessException.build("warehouse.reprint.startDateAfterEndDate", DateUtil.format(startDate, "yyyy-MM-dd HH:mm:ss"), DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss"));
        }

        EntityWrapper<PrintLog> entityWrapper = new EntityWrapper<>();
        PrintLog printLog = new PrintLog();
        printLog.setSite(site);
        printLog.setPrinter(printer);
        printLog.setPrintTemplate(printTemplate);
        printLog.setCreateUserBo(createUser != null ? HandleEnum.USER.getHandle(site, createUser) : null);
        printLog.setIsReprint("N");
        entityWrapper.setEntity(printLog);
        if (startDate != null) {
            entityWrapper.ge("created_date_time", startDate);
        }
        if (endDate != null) {
            entityWrapper.lt("created_date_time", endDate);
        }
        return printLogService.selectPage(frontPage.getPagePlus(), entityWrapper);
    }

    public void reprint(PrintDto printDto) {
        printService.print(printDto);
    }
}
